package com.projectfile.config;

import org.apache.catalina.filters.CorsFilter;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    //注册到spring容器中
//    @Bean
//    @LoadBalanced
//    public CorsFilter corsFilter(){
//        //初始化cors配置对象
//        CorsConfiguration configuration = new CorsConfiguration();
//        //配置需要跨域的地址,如果要携带cookie,不能写*，*：代表所有域名都可以跨域访问
//        configuration.addAllowedOrigin("http://localhost.leyou.com");
//        //配置是否允许带cookie
//        configuration.setAllowCredentials(true);
//        //配置所要携带的请求方法，*代表所有的请求的方法：Get put post delete
//        configuration.addAllowedMethod("*");
//        //配置所要携带的头信息
//        configuration.addAllowedHeader("*");
//
//        //初始化cors配置源对象，添加映射路径，拦截一些请求
//        UrlBasedCorsConfigurationSource urlBasedConfigurationSource=new UrlBasedCorsConfigurationSource();
//        urlBasedConfigurationSource.registerCorsConfiguration("/**",configuration);
//
//        //返回corsFilter实例，参数：cors配置源对象
//        return new CorsFilter();
//    }

}


